swap用法

#C#

Posted by Phyxsius on 2023-09-14


internal class Program
{
    private static void Main(string[] args)
    {
        int[] ary = { 1, 2};

        Console.WriteLine("原始陣列內容:");
        showArray(ary);

        swap(ary, 0, 1);

        Console.WriteLine("交換後陣列內容:");
        showArray(ary);
    }

    //兩個值互相交換
    static void swap(int[] ary, int value1, int value2)
    {
        int tmp = ary[value2];
        ary[value2] = ary[value1];
        ary[value1] = tmp;
    }

    //顯示陣列內容
    static void showArray(int[] ary)
    {
        for (int i=0; i<ary.Length; i++)
        {
            Console.Write(ary[i] + ",");
        }
        Console.WriteLine();
    }
}

#C#







Related Posts

[JavaScript] ES6:Template Literals 樣板字面值

[JavaScript] ES6:Template Literals 樣板字面值

Day 148

Day 148

30-Day LeetCoding Challenge 2020 April Week 1 || Leetcode 解題

30-Day LeetCoding Challenge 2020 April Week 1 || Leetcode 解題


Comments